package de.zainodis.commons.model.binding;

import java.util.HashMap;
import java.util.Map;

/**
 * Only able to handle one-leveled datapaths which serves as the key of
 * accessing a map's values and manipulating them.
 * 
 * @author zainodis
 * 
 */
public class MapDataSource implements IDataSource {

   // The original datapath from which this Datasource was created
   private IDatapath path;
   // Required to actually modify the properties value
   private Map<Object, Object> map;

   public MapDataSource(Map<Object, Object> targetMap, IDatapath path) {
	 this.map = targetMap;
	 this.path = path;
   }

   @Override
   public void setValue(Object value) {
	 if (map == null) {
	    map = new HashMap<Object, Object>();
	 }
	 map.put(path, value);
   }

   @Override
   public Object getValue() {
	 if (map != null) {
	    return map.get(path);
	 } else {
	    return null;
	 }
   }

   @Override
   public IDatapath getPath() {
	 return path;
   }

}
